package Action;

import java.util.List;
import java.util.Map;

import proxy.ProxyFactory;
import Beans.Admin;
import Beans.Agent;
import Beans.Branch;
import Beans.Goods;
import Beans.Package;
import Beans.Terminal;
import Service.BoxAndPackageService;
import Service.BoxAndPackageServiceInterface;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BoxAndPackageAction extends ActionSupport {

	private int terminalID;
	private int branchID;
	private int packageID;
	private int goodsID;
	private int boxNum;
	private int type;
	private int problemID;
	private int status;
	private int agentID;
	private int operate;// 包裹操作
	private int detained;// 滞留等级
	private String flag;
	private String comment;
	private String reason;// 未收到短信原因
	private String newPhone;//新手机号码

	private Package p = null;

	List<Package> packages = null;
	List<Agent> agents = null;
	List<Branch> branchs = null;
	List<Terminal> terminals = null;
	List<Goods> goodsList = null;

	private BoxAndPackageServiceInterface boxPkgService = (BoxAndPackageServiceInterface) ProxyFactory
			.getProxyInstance(new BoxAndPackageService());

	/**
	 * 返回相应的JSP页面
	 * 
	 * @return
	 */
	public String returnJSP() {
		if ("sign".equals(flag)) {
			return "sign";
		} else {
			return "error";
		}

	}

	/**
	 * 返回滞留包裹查询结果
	 * 
	 * @return
	 */
	public String getUnTakenPakcages() {
		packages = boxPkgService.getUnTakenPakcages(agentID, branchID,
				terminalID, detained);
		return "success";
	}

	/**
	 * 滞留包裹查询
	 * 
	 * @return
	 */
	public String unTakenParcelSearch() {
		agents = boxPkgService.getAllAgent(agents);
		return "unTaken";
	}

	/**
	 * 根据代理商获取设备网点
	 * 
	 * @return
	 */
	public String getBranchByAgent() {
		branchs = boxPkgService.getBranchByAgent(agentID);
		return "selectBranch";
	}

	/**
	 * 根据设备网点获取设备
	 * 
	 * @return
	 */
	public String getTerminalByBranch() {
		terminals = boxPkgService.getTerminalByBranch(branchID);
		return "selectTerminal";
	}

	/**
	 * 解锁包裹
	 */
	public void unLockParcel() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		boxPkgService.unLockParcel(p, packageID, admin);
	}

	/**
	 * 锁定包裹
	 */
	public void signParcel() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		boxPkgService.signParcel(newPhone, p, packageID, admin, problemID, operate,
				detained, reason, comment);
	}

	/**
	 * 处理包裹并写入日志表
	 * 
	 * @return
	 */
	public String dealPackage() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		packages = boxPkgService.dealPackage(packages, p, packageID, admin);
		return "success";
	}

	/**
	 * 清空包裹
	 * 
	 * @return
	 */
	public String taken() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		if(packageID!=0){
			boxPkgService.take(terminalID, boxNum, type, packageID,admin);
		}
		if(goodsID!=0){
			boxPkgService.takeGoods(terminalID, boxNum, type,goodsID, admin);
		}
		// boxDao.setBoxAvailable(terminalID, boxNum, type);
		// packageDao.setPackageTaken(terminalID, boxNum);
		return "success";

	}

	/**
	 * 不知道什么包裹
	 * 
	 * @return
	 */
	public String back() {
		boxPkgService.back(terminalID, boxNum, type);
		// boxDao.setBoxAvailable(terminalID, boxNum, type);
		// packageDao.setPackageBack(terminalID, boxNum);
		return "success";

	}

	/**
	 * 清空该终端所有在使用的箱格
	 * 
	 * @return
	 */
	public String clearAllUseBox() {
		boxPkgService.clearAllUseBox(terminalID, type);
		// List<Box> list = boxDao.getAllUseBoxOfTerminal(terminalID);
		// for (Box box : list) {
		// boxDao.setBoxAvailable(terminalID, box.getBoxNum(), type);
		// packageDao.setPackageTaken(terminalID, box.getBoxId());
		// }
		return "success";
	}

	/**
	 * 置位包裹
	 * 
	 * @return
	 */
	public String unTaken() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		if(packageID==1){
			boxPkgService.unTaken(terminalID, boxNum, type, admin);
		}
		if(goodsID==1){
			boxPkgService.unTakenGoods(terminalID, boxNum, type);
		}
		// boxDao.setBoxOccupation(terminalID, boxNum);
		// packageDao.setPackageUnTaken(terminalID, boxNum);
		return "success";
	}

	/**
	 * 查询箱格存物详情
	 * 
	 * @return
	 */
	public String getBoxStoreDetails() {
		packages = boxPkgService.getStorePackage(terminalID, boxNum);
		goodsList = boxPkgService.getStoreGoods(terminalID, boxNum);
		if (!packages.isEmpty()) {
			return "success";
		} else if (!goodsList.isEmpty()) {
			return "goodsList";
		} else {
			return "error";
		}
	}

	public int getBoxNum() {
		return boxNum;
	}

	public void setBoxNum(int boxNum) {
		this.boxNum = boxNum;
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getPackageID() {
		return packageID;
	}

	public void setPackageID(int packageID) {
		this.packageID = packageID;
	}

	public Package getP() {
		return p;
	}

	public void setP(Package p) {
		this.p = p;
	}

	public List<Package> getPackages() {
		return packages;
	}

	public void setPackages(List<Package> packages) {
		this.packages = packages;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public int getProblemID() {
		return problemID;
	}

	public void setProblemID(int problemID) {
		this.problemID = problemID;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getOperate() {
		return operate;
	}

	public void setOperate(int operate) {
		this.operate = operate;
	}

	public int getDetained() {
		return detained;
	}

	public void setDetained(int detained) {
		this.detained = detained;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public int getBranchID() {
		return branchID;
	}

	public void setBranchID(int branchID) {
		this.branchID = branchID;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public int getGoodsID() {
		return goodsID;
	}

	public void setGoodsID(int goodsID) {
		this.goodsID = goodsID;
	}

	public List<Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public String getNewPhone() {
		return newPhone;
	}

	public void setNewPhone(String newPhone) {
		this.newPhone = newPhone;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
